package com.example.demo.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//@Component
//@Aspect
public class AopTimes {

    //PointCut 将公共的切入点抽取出来,要是其他类用就改为public
//    @Pointcut("execution(* com.example.demo.service.*.* (..))")
    private void add(){};

    //环绕:
//    @Around("execution(* com.example.demo.service.*.* (..))")
    public Object equals(ProceedingJoinPoint joinPoint) throws Throwable {
        //1.记录开始时间
        long startTime = System.currentTimeMillis();
        //2.调用原始方法
        Object result = joinPoint.proceed();
        //3.记录结束时间  并计算方法运行耗时
        long endTime = System.currentTimeMillis();
        System.out.println(joinPoint.getSignature() + "方法执行耗时:" + (endTime-startTime));
        return result;
    }


    @Before("add()")
    public void before() {
        System.out.println("此方法在目标函数执行前执行");
    }

    @After("add()")
    public void after() {
        System.out.println("在目标方法后执行, 无论目标方法是否异常都会执行");
    }

    @AfterReturning("add()")
    public void afterReturning() {
        System.out.println("返回通知,在目标方法后被执行,有异常不会执行");
    }

    @AfterThrowing("add()")
    public void afterThrowing() {
        System.out.println("在目标方法执行异常后执行");
    }

}
